﻿using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using GSQ.BusinessEntities;
using GSQ.DataAccess.DataSetTableAdapters;

namespace GSQ.DataAccess
{
    public class FacturasPagosDA
    {
        private FACTURAS_PAGOSTableAdapter Adapter = new FACTURAS_PAGOSTableAdapter();
        private V_FACTURAS_PAGOSTableAdapter vAdapter = new V_FACTURAS_PAGOSTableAdapter();
        private DataSet.FACTURAS_PAGOSDataTable entities;
        private DataSet.FACTURAS_PAGOSRow entity;

        public decimal Insert(FACTURAS_PAGOS value)
        {
            using (SqlConnection cnn = new SqlConnection(Connection.GetConnectionString))
            {
                try
                {
                    cnn.Open();
                    Adapter.Connection = cnn;

                    entities = new DataSet.FACTURAS_PAGOSDataTable();

                    entity = entities.NewFACTURAS_PAGOSRow();

                    entity.ID = value.ID;
                    entity.ID_FACTURA = value.ID_FACTURA;
                    entity.ID_TIPO = value.ID_TIPO;
                    if (value.ID_BANCO != null)
                        entity.ID_BANCO = value.ID_BANCO.Value;
                    entity.ID_USUARIO = value.ID_USUARIO;
                    if (value.NRO_DOCUMENTO != null && value.NRO_DOCUMENTO != string.Empty)
                        entity.NRO_DOCUMENTO = value.NRO_DOCUMENTO;
                    if (value.PORTADOR != null && value.PORTADOR != string.Empty)
                        entity.PORTADOR = value.PORTADOR;
                    if (value.TELEFONO != null && value.TELEFONO != string.Empty)
                        entity.TELEFONO = value.TELEFONO;
                    entity.FECHA = value.FECHA;
                    entity.MONTO = value.MONTO;
                    entities.AddFACTURAS_PAGOSRow(entity);
                    Adapter.Update(entities);

                    value.ID = entity.ID;
                    return value.ID;
                }
                finally
                {
                    if (cnn.State != System.Data.ConnectionState.Closed)
                        cnn.Close();
                }
            }
        }

        public decimal Update(FACTURAS_PAGOS value)
        {
            using (SqlConnection cnn = new SqlConnection(Connection.GetConnectionString))
            {
                try
                {
                    cnn.Open();
                    this.Adapter.Connection = cnn;

                    entities = Adapter.GetDataByID(value.ID);
                    entity = entities.FirstOrDefault();


                    //entity.ID_FACTURA = value.ID_FACTURA;
                    entity.ID_TIPO = value.ID_TIPO;
                    if (value.ID_BANCO != null)
                        entity.ID_BANCO = value.ID_BANCO.Value;
                    entity.ID_USUARIO = value.ID_USUARIO;
                    if (value.NRO_DOCUMENTO != null && value.NRO_DOCUMENTO != string.Empty)
                        entity.NRO_DOCUMENTO = value.NRO_DOCUMENTO;
                    if (value.PORTADOR != null && value.PORTADOR != string.Empty)
                        entity.PORTADOR = value.PORTADOR;
                    if (value.TELEFONO != null && value.TELEFONO != string.Empty)
                        entity.TELEFONO = value.TELEFONO;
                    entity.FECHA = value.FECHA;
                    entity.MONTO = value.MONTO;

                    this.Adapter.Update(entities);

                    return entity.ID;
                }
                finally
                {
                    if (cnn.State != System.Data.ConnectionState.Closed)
                        cnn.Close();
                }
            }
        }

        public FACTURAS_PAGOS GetById(decimal id)
        {
            using (SqlConnection cnn = new SqlConnection(Connection.GetConnectionString))
            {
                try
                {
                    cnn.Open();
                    this.vAdapter.Connection = cnn;

                    return GetEntity(this.vAdapter.GetDataByID(id).FirstOrDefault());
                }
                finally
                {
                    if (cnn.State != System.Data.ConnectionState.Closed)
                        cnn.Close();
                }
            }
        }

        public List<FACTURAS_PAGOS> GetAll()
        {
            using (SqlConnection cnn = new SqlConnection(Connection.GetConnectionString))
            {
                try
                {
                    cnn.Open();
                    this.vAdapter.Connection = cnn;

                    List<FACTURAS_PAGOS> lst = new List<FACTURAS_PAGOS>();
                    foreach (DataSet.V_FACTURAS_PAGOSRow rw in this.vAdapter.GetData())
                    {
                        lst.Add(this.GetEntity(rw));
                    }
                    cnn.Close();
                    return lst;
                }
                finally
                {
                    if (cnn.State != System.Data.ConnectionState.Closed)
                        cnn.Close();
                }
            }
        }

        public List<FACTURAS_PAGOS> GetByIdFactura(decimal id)
        {
            using (SqlConnection cnn = new SqlConnection(Connection.GetConnectionString))
            {
                try
                {
                    cnn.Open();
                    this.vAdapter.Connection = cnn;

                    List<FACTURAS_PAGOS> lst = new List<FACTURAS_PAGOS>();
                    foreach (DataSet.V_FACTURAS_PAGOSRow rw in this.vAdapter.GetDataByIdFactura(id))
                    {
                        lst.Add(this.GetEntity(rw));
                    }
                    cnn.Close();
                    return lst;
                }
                finally
                {
                    if (cnn.State != System.Data.ConnectionState.Closed)
                        cnn.Close();
                }
            }
        }

        public List<FACTURAS_PAGOS_TIPOS> GetAllFacturasPagosTipos()
        {
            using (SqlConnection cnn = new SqlConnection(Connection.GetConnectionString))
            {
                try
                {
                    cnn.Open();
                    FACTURAS_PAGOS_TIPOSTableAdapter adap = new FACTURAS_PAGOS_TIPOSTableAdapter();
                    adap.Connection = cnn;

                    List<FACTURAS_PAGOS_TIPOS> lst = new List<FACTURAS_PAGOS_TIPOS>();
                    foreach (DataSet.FACTURAS_PAGOS_TIPOSRow rw in adap.GetData())
                    {
                        lst.Add(new FACTURAS_PAGOS_TIPOS { ID = rw.ID, TIPO = rw.TIPO });
                    }
                    cnn.Close();
                    return lst;
                }
                finally
                {
                    if (cnn.State != System.Data.ConnectionState.Closed)
                        cnn.Close();
                }
            }
        }

        private FACTURAS_PAGOS GetEntity(DataSet.V_FACTURAS_PAGOSRow rw)
        {
            FACTURAS_PAGOS fp = null;
            if (rw != null)
            {
                fp = new FACTURAS_PAGOS();
                fp.ID = rw.ID;
                fp.ID_FACTURA = rw.ID_FACTURA;
                fp.ID_TIPO = rw.ID_TIPO;
                fp.FACTURAS_PAGOS_TIPOS = new FACTURAS_PAGOS_TIPOS { ID = rw.ID_TIPO, TIPO = rw.TIPO };
                if (!rw.IsID_BANCONull())
                {
                    fp.ID_BANCO = rw.ID_BANCO;
                    fp.BANCOS = new BANCOS { ID = rw.ID_BANCO, BANCO = rw.BANCO };
                }
                fp.ID_USUARIO = rw.ID_USUARIO;
                if (!rw.IsNRO_DOCUMENTONull())
                    fp.NRO_DOCUMENTO = rw.NRO_DOCUMENTO;
                if (!rw.IsPORTADORNull())
                    fp.PORTADOR = rw.PORTADOR;
                if (!rw.IsTELEFONONull())
                    fp.TELEFONO = rw.TELEFONO;
                fp.FECHA = rw.FECHA;
                fp.MONTO = rw.MONTO;

            }
            return fp;

        }
    }
}
